<?php

/**
 * Core_Response_Abstract
 *
 * @author JianJian <silverd@sohu.com>
 * $Id: Abstract.php 142 2012-10-21 14:40:59Z silverd30@gmail.com $
 */

abstract class Core_Response_Abstract
{
    protected $_body;

    protected static $_instances = array();
    final public static function getInstance()
    {
        $class = get_called_class();

        if (!isset(self::$_instances[$class])) {
            self::$_instances[$class] = new $class();
        }

        return self::$_instances[$class];
    }

    public function setBody($content)
    {
        $this->_body = (string) $content;

        return $this;
    }

    public function appendBody($content)
    {
        $this->_body .= (string) $content;

        return $this;
    }

    public function prependBody($content)
    {
        $this->_body = (string) $content . $this->_body;

        return $this;
    }

    public function clearBody()
    {
        $this->_body = null;
    }

    public function getBody()
    {
        return $this->_body;
    }

    public function outputBody()
    {
        echo $this->_body;
    }

    // Alias to outputBody()
    public function response()
    {
        $this->outputBody();
    }

    public function __toString()
    {
        return $this->getBody();
    }
}